/*#################################################

LiveForm | v1.2 
Max Felker | max@bigroomstudios.com

Maintains form elements on a page

#################################################*/
 
// START Class
var LiveForm = Class.create({

	initialize: function(config) { 

		// form container
		this.container = $(config.container);
		this.form_name = this.container.name;
		
		this.container.on('submit',function(event) {
			this.submit(event);
		}.bind(this));

		// input list (list of elements in form)
		this.input_list = this.container.getElements();
		
		// inputs hash (hash object for pulling input values)
		this.inputs = $H();		
		
		// valid & invalid hashes
		this.valid_inputs = $H();
		this.invalid_inputs = $H();
		
		// input observes 
		this.input_list.each(function(input) {
		
			this.inputs.set(input.name,input.value);

		}.bind(this)); // END each inputs

		// add to live
		live.forms.set(this.container.name,this);

	}, // end init

	// loops through each input and validates input value
	validate: function() {
	
		this.input_list.each(function(input) {
		
			this.inputs.set(input.name,input.value);
			
			if(input.type!='submit') {
				this.validate_input(input);
			}
			
		}.bind(this));
		
		// if invalid input count is 0, form is valid
		if(this.invalid_inputs.keys().length==0) {
			return true;
		} else {
			return false;
		}	

	},

	// validate single input
	validate_input: function(input) {
	
		// set current input
		this.current_input = this.container[input.name];
		
		//$('log').innerHTML += input.name+'<br/>';
			
		// unset from valid / invalid hashes
		this.valid_inputs.unset(input.name);
		this.invalid_inputs.unset(input.name);
		
		if(this.current_input[0]) { 
		// groups inputs [radio,checkbox,select]
			
			// if current input is select dropdown
			if(this.current_input.type=="select-one" && this.current_input.hasClassName('valid-select')) {

				// if option selected is empty (the first one in the set), set to invalid
				if(this.current_input.value.empty()) {	
					this.invalid_inputs.set(this.current_input.name,'select');
					this.current_input.addClassName('invalid');
				} else {
					this.valid_inputs.set(this.current_input.name);	
					this.current_input.removeClassName('invalid');
				}
				
			// else if current input is radio
			} else if(this.current_input[0].hasClassName('valid-bit')) {

				// put current input into array
				var radios = $A(this.current_input);

				// loop through radios
				radio_is_checked = false;
				radios.each(function(radio) {

					// if it's checked, return vali, else invalid
					if(radio.checked) {
						this.valid_inputs.set(this.current_input[0].name);	
						this.invalid_inputs.unset(this.current_input[0].name);
						throw $break;
					} else {
						this.invalid_inputs.set(radio.name,'radio');
						radio.addClassName('invalid');
					}

				}.bind(this));

			} 
		
		// single inputs
		} else if(!this.current_input[0]) {
		
			// checkbox validation
			if(this.current_input.hasClassName('valid-checkbox')) {
			
				if(!this.current_input.checked) {	
					this.invalid_inputs.set(this.current_input.name,'valid-checkbox');
					this.current_input.addClassName('invalid');
				} else {
					this.valid_inputs.set(this.current_input.name);	
					this.current_input.removeClassName('invalid');
				}
			
			// all other input validation
			} else {
		
				// loop through live.regexp rules
				live.regex.keys().each(function(validation_class) {

					// if current input has live.regexp rules
					if(this.current_input.hasClassName('valid-'+validation_class)) {
					
						// check for empty validation
						if(validation_class=="empty") {	
							if(this.current_input.value.empty()) {	
								this.invalid_inputs.set(this.current_input.name,validation_class);
								this.current_input.addClassName('invalid');
							} else {
								this.valid_inputs.set(this.current_input.name);	
								this.current_input.removeClassName('invalid');
							}
						// else if input value passes live.regex
						} else if(live.regex.get(validation_class).test(this.current_input.value)) {
							if(this.current_input.value.empty()) {	
								this.invalid_inputs.set(this.current_input.name,validation_class);
								this.current_input.addClassName('invalid');
							} else {
								this.valid_inputs.set(this.current_input.name);	
								this.current_input.removeClassName('invalid');
							}
						// else it's in valid
						} else {
							this.invalid_inputs.set(this.current_input.name,validation_class);
							this.current_input.addClassName('invalid');
						}
					
						throw $break;
					
					}

				}.bind(this)); // END regexp loop
			
			}

		} // END if single input validation


	}, // END validate_input 

	submit: function(event) {
	
		this.container.disable();
				
		// validate the submission
		if(this.validate()) {

			this.container.enable();
			
			if(this.custom_success_function) {
			
				Event.stop(event);
				this.custom_success_function();
				
			} else {

				return true;
			
			}
			
		} else {
		
			Event.stop(event); 
			this.container.enable();
		
			if(this.custom_failure_function) {
			
				this.custom_failure_function();
				
			} else {

				return false;
			
			}

		}

	},
	
	success: function(custom_function) {
	
		this.custom_success_function = custom_function.methodize();
	
	},
	
	failure: function(custom_function) {
	
		this.custom_failure_function = custom_function.methodize();
	
	},
	
	request: function(config) {
	
		this.container.request(config);
	
	},
	
	reset: function() { 
	
		this.container.reset();
	
	}
	


}); // END Class

// on dom load, add forms to live
document.on('dom:loaded', function() {

	$$('.live-form').each(function(form) {
		if(form) {
			new LiveForm({container:form.id});
		}
	});
	
});
